package day_2022_7_to_8.day48;

/**
 * @author haomin
 * @date 2022/08/11 12:07
 **/
public class Day26_1_Num1417_reformat {
    class Solution {
        public String reformat(String s) {
            int letter = 0,digit = 0;
            for(int i = 0;i < s.length();i++){
                if(Character.isDigit(s.charAt(i))) digit++;
                if(Character.isLetter(s.charAt(i))) letter++;
            }
            if(Math.abs(digit-letter) > 1) return ""; // 先排除不能组合的字符串
            char[] data = new char[s.length()];
            int i = 0,j = 1;                   // 双指针 一个指向数字索引，一个指向字母索引
            if(digit < letter){ i = 1;j = 0;}  // 当字母多时，字母从0开始，否则数字从0开始
            for(int k = 0;k < s.length();k++){
                char c = s.charAt(k);
                if(Character.isDigit(c)) {
                    data[i] = c;i+=2;
                }
                if(Character.isLetter(c)) {
                    data[j] = c;j+=2;
                }
            }
            return new String(data);
        }
    }
}